{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.2\n",
      "1.3\n",
      "-1.3\n",
      "1627730\n",
      "1627700\n",
      "1628000\n"
     ]
    }
   ],
   "source": [
    "# 对浮点数执行指定精度的舍入运算\n",
    "# round(value, ndigits)\n",
    "print(round(1.23, 1))\n",
    "print(round(1.27, 1))\n",
    "print(round(-1.27, 1))\n",
    "a = 1627731\n",
    "print(round(a, -1))\n",
    "print(round(a, -2))\n",
    "print(round(a, -3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.3\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 精确的浮点数运算(容忍一定的性能损耗)\n",
    "from decimal import Decimal\n",
    "a = Decimal('4.2')\n",
    "b = Decimal('2.1')\n",
    "print(a+b)\n",
    "print(a+b==Decimal('6.3'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.10\n"
     ]
    }
   ],
   "source": [
    "# 数字格式化输出 [<>^]?width[,]?(.digits)?\n",
    "print(f'{b:.2f}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0b10011010010\n",
      "0o2322\n",
      "0x4d2\n",
      "10011010010\n",
      "2322\n",
      "4d2\n",
      "1234\n"
     ]
    }
   ],
   "source": [
    "# 进制转换\n",
    "x = 1234\n",
    "print(bin(x))\n",
    "print(oct(x))\n",
    "print(hex(x))\n",
    "\n",
    "# 不想输出 0b , 0o 或者 0x 的前缀\n",
    "print(format(x, 'b'))\n",
    "print(format(x, 'o'))\n",
    "print(format(x, 'x'))\n",
    "\n",
    "# 以不同的进制转换整数字符串\n",
    "print(int('4d2', 16))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "inf -inf nan\n"
     ]
    }
   ],
   "source": [
    "# 无穷大与NaN\n",
    "a = float('inf')\n",
    "b = float('-inf')\n",
    "c = float('nan')\n",
    "print(a,b,c)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "qwenv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
